﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ZaxLib.Web.Writers.Nodes;

namespace ZaxLib.Web.UI.WebControls
{
	[DefaultProperty("Text")]
	[ToolboxData("<{0}:DragPanel runat=server></{0}:DragPanel>")]
	public class DropPanel : ZaxControl
	{
		#region Data Structures

		class ListItem
		{
			public String Text;
			public String CssClass;

			public ListItem(String inText)
			{
				Text = inText;
				CssClass = String.Empty;
			}

			public ListItem(String inText, String css)
			{
				Text = inText;
				CssClass = css;
			}
		}

		#endregion

		#region Parameters

		private ArrayList _items = null;
		public ArrayList Items
		{
			get
			{
				if (_items == null)
				{
					_items = new ArrayList();
					if (Text.Trim().Length > 0)
					{
						String[] aItems = Text.Split(',');
						foreach (String item in aItems)
						{
							AddItem(item);
						}
					}
				}

				return _items;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String HoverClass
		{
			get
			{
				String s = ViewState["HoverClass"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["HoverClass"] = value;
			}
		}

		#endregion

		#region Control Writers

		protected override void ControlWrite(HtmlTextWriter writer)
		{
			base.ControlWrite(writer);
			String RemoveMeClass = "placeholder";

			writer.AddAttribute(HtmlTextWriterAttribute.Id, ID);
			if (CssClass.Trim().Length > 0)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
			}
			writer.RenderBeginTag(HtmlTextWriterTag.Ul);
			if (Items.Count == 0)
			{
				AddItem("Place Items Here", RemoveMeClass);
			}

			foreach (ListItem item in Items)
			{
				writer.Flush();
				if (item.CssClass != String.Empty)
				{
					writer.AddAttribute(HtmlTextWriterAttribute.Class, item.CssClass);
				}
				writer.RenderBeginTag(HtmlTextWriterTag.Li);
				writer.Write(item.Text);
				writer.RenderEndTag();
				writer.WriteLine();
			}
			writer.RenderEndTag();

			// Write jQuery code for dropping items on this element
			INode top = CodeWriter.CreateTopNode();
			INode node = CodeWriter.CreateReadyFunction(top);
			CodeWriter.AddChild(ID, node).SelectorId();
			CodeWriter.AddChild("droppable").Joined();
			node = CodeWriter.AddParam("").ObjectDef();
			if(HoverClass.Trim().Length > 0)
			{
				CodeWriter.AddParam("hoverClass", String.Format("\".{0}\"", HoverClass), node).VarDef();
			}

			INode param = CodeWriter.AddParam("drop", node).VarDef();
			INode func = CodeWriter.AddChild("function", param).Function();
			CodeWriter.AddParam("event", func).VarDef();
			CodeWriter.AddParam("ui", func).VarDef();
			CodeWriter.AddChild("this", func).Selector();
			node = CodeWriter.AddChild("find").Joined();
			CodeWriter.AddParam(String.Format("\".{0}\"", RemoveMeClass)).VarDef();
			CodeWriter.AddChild("remove", node).Joined();
			CodeWriter.AddParam("").VarDef();

			CodeWriter.AddChild("\"<li></li>\"", func).Selector();
			node = CodeWriter.AddChild("text").Joined();
			CodeWriter.AddParam("ui.draggable.text()").VarDef();
			CodeWriter.AddChild("appendTo").Joined();
			CodeWriter.AddParam("this").VarDef();

			CodeWriter.AddChild("ui.draggable.remove();", func).RawCode();
			//CodeWriter.AddParam("drop", "function(event, ui){ $(this).find(\".placeholder\").remove(); ui.draggable.remove(); }", node).VarDef();

		}

		#endregion

		#region Render Methods

		protected override void RenderContents(HtmlTextWriter writer)
		{
			base.RenderContents(writer);
		}

		#endregion

		#region Data Methods

		public void AddItem(String item)
		{
			AddItem(item, String.Empty);
		}

		public void AddItem(String item, String css)
		{
			ListItem lItem = new ListItem(item, css);
			_items.Add(lItem);
		}

		#endregion
	}
}
